package com.jinlejia.bbs.logic;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.GambitBean;
import com.jinlejia.bbs.bean.GambitListBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.http.entity.GambitEntity;
import com.jinlejia.bbs.http.entity.UserEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/3/26.
 */

public class HotGambitLogic {
    private final Context context;
    private final GambitListBean bean;
    private final LogicCallBack callBack;

    public HotGambitLogic(Activity activity, GambitListBean bean, LogicCallBack callBack) {
        this.context = activity;
        this.bean = bean;
        this.callBack = callBack;
    }

    private int pageIndex;
    public void refresh(){
        pageIndex = 1;
        loadData(pageIndex);
    }

    public void load(){
        pageIndex = pageIndex+1;
        loadData(pageIndex);
    }

    private void loadData(final int pageIndex){
        JSONObject json = new JSONObject();
        json.put("pageIndex",pageIndex);
        json.put("pageSize",10);
        httpCallForm(BBSConfig.listHotTopic,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false);
                    }
                    @Override
                    public void onError(Exception e) {
                        if (callBack==null)return;
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        if (callBack==null)return;
                        TypeReference<PageEntity<GambitEntity>> reference = new TypeReference<PageEntity<GambitEntity>>() {
                        };
                        PageEntity<GambitEntity> response = parse.parsePageEntity(reference);
                        List<GambitEntity> data = response.getData();
                        ArrayList<GambitBean> list = new ArrayList<>();
                        for (GambitEntity entity : data) {
                            GambitBean bean = new GambitBean();
                            //组装 话题
                            TopicBean topic = new TopicBean();
                            String topicTitle = entity.getTopicTitle();
                            topic.setTopicTitle(topicTitle);
                            topic.setTopicCode(entity.getTopicCode());
                            bean.setTopic(topic);


                            InformationBean info = null;
                            if (!TextUtils.isEmpty(entity.getContent()) || !TextUtils.isEmpty(entity.getContent())){
                                info = new InformationBean();
                                if (entity.getContentType()==1){
                                    info.setMessageContent(entity.getContent()); //回答内容
                                }else if (entity.getContentType()==2){
                                    info.setSoundContent(SoundBean.create(entity.getContent())); //回答内容
                                }
                                info.setReviewCount(entity.getReviewNum()); //回答阅读次数
                                info.setLikeCount(entity.getSupportCount()); //回答点赞次数
                                info.setOwnLike(entity.getIsUser() == 1); //是否自己已经点赞


                                //region 组装回答用户
                                BBSUserBean user = new BBSUserBean();
                                user.setUserPin(entity.getUserPin()); //回答用户PIN
                                user.setUserName(entity.getUserName());//回答用户姓名
                                user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl())); //回答用户头像
                                if (TextUtils.isEmpty(entity.getAnswerStoreName())){
                                    user.setUserDuty(entity.getAnswerStoreName()); //回答用户职务
                                    user.setUserAddress(entity.getAnswerStoreName());
                                }else {
                                    user.setUserDuty(entity.getUserTitleLevel()); //回答用户职务
                                    user.setUserAddress(entity.getUserTitleLevel());
                                }

                                info.setUser(user);
                                info.setAnswerCode(entity.getAnswerCode());
                                //endregion
                                List<UserEntity> recordsUserResults = entity.getAttitudesRecordsUserResults();
                                List<BBSUserBean> likeList = new ArrayList<>();
                                if (recordsUserResults!=null){
                                    for (UserEntity userEntity:recordsUserResults) {
                                        String imageUrl = userEntity.getImageUrl();
                                        String pin = userEntity.getPin();
                                        BBSUserBean likeUser = new BBSUserBean();
                                        likeUser.setUserAvatar(BBSConfig.headerUrl(imageUrl));
                                        likeUser.setUserPin(pin);
                                        likeList.add(likeUser);
                                    }
                                }
                                info.setLikeUserList(likeList);
                            }
                            bean.setInfo(info);

                            list.add(bean);
                        }
                        if (pageIndex==1){
                            bean.setGambitBeans(list);
                        }else {
                            bean.getGambitBeans().addAll(list);
                        }
                        boolean pageAvailable = response.getNextPageAvailable();
                        callBack.onResult(pageAvailable);
                    }
                }));
    }



    public interface LogicCallBack {
        void onResult(boolean hasNextPage);
    }
}
